<?php
namespace app\admin\controller;
use app\admin\controller\AdminController;
/**
 * 板块设置
 */
class PlateController extends AdminController {
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'menu' => array(
                    array(
                        'name' => '板块列表',
                        'url' => url('index'),
                        'icon' => 'bars',
                    ),
                    array(
                        'name' => '添加板块',
                        'url' => url('add'),
                        'icon' => 'plus',
                    ),
                ),
            );
    }

	/**
     * 列表
     */
    public function index(){
    	//设置目录导航
    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'板块设置');
        $this->assign('list', target('forum/Plate')->loadList());
        $this->adminDisplay();
    }

    /**
     * 添加
     */
    public function add(){
    	if(!IS_POST){
	    	//设置目录导航
	    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'板块设置');
	    	$this->adminCrumb[] = array('url'=>url('add'),'name'=>'板块添加');
	        $this->assign('list', target('forum/Plate')->loadList());
            $this->assign('modelList', target('forum/Plate')->getModel());
	        $this->assign('name','添加');
	        $this->adminDisplay('info');
        }else{
            if(target('forum/Plate')->saveData('add')){
                $this->success('板块添加成功！',url('index'));
            }else{
                $msg = target('forum/Plate')->getError();
                if(empty($msg)){
                    $this->error('板块添加失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 编辑
     */
    public function edit(){
    	if(!IS_POST){
    		$plateId = request('get.plate_id','','intval');
            if(empty($plateId)){
                $this->error('参数不能为空！');
            }
            $model = target('forum/Plate');
            $info = $model->getInfo($plateId);
            if(!$info){
                $this->error($model->getError());
            }
	    	//设置目录导航
	    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'板块设置');
	    	$this->adminCrumb[] = array('url'=>url('edit',array('plate_id'=>$plateId)),'name'=>'板块编辑');
	        $this->assign('list', target('forum/Plate')->loadList());
            $this->assign('modelList', target('forum/Plate')->getModel());
	        $this->assign('name','编辑');
	        $this->assign('info',$info);
	        $this->adminDisplay('info');
        }else{
            if(target('forum/Plate')->saveData('edit')){
                $this->success('板块编辑成功！',url('index'));
            }else{
                $msg = target('forum/Plate')->getError();
                if(empty($msg)){
                    $this->error('板块编辑失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 权限
     */
    public function purview(){
        if(!IS_POST){
            $plateId = request('get.plate_id','','intval');
            if(empty($plateId)){
                $this->error('参数不能为空！');
            }
            $model = target('forum/Plate');
            $info = $model->getInfo($plateId);
            if(!$info){
                $this->error($model->getError());
            }
            //设置目录导航
            $this->adminCrumb[] = array('url'=>url('index'),'name'=>'板块设置');
            $this->adminCrumb[] = array('url'=>url('purview',array('plate_id'=>$plateId)),'name'=>'板块权限');
            $this->assign('info',$info);
            $this->assign('groupList', target('forum/UserGroup')->loadList());
            $this->adminDisplay('purview');
        }else{
            $post = request('post.');
            $data = array();
            $data['purview'] = serialize($post['purview']);
            $data['moderator'] = $post['moderator'];
            $data['plate_id'] = $post['plate_id'];
            if(target('forum/Plate')->save($data) !== false){
                $this->success('权限编辑成功！',url('index'));
            }else{
                $msg = target('forum/Plate')->getError();
                if(empty($msg)){
                    $this->error('权限编辑失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 删除
     */
    public function del(){
        $plateId = request('post.data');
        if(empty($plateId)){
            $this->error('参数不能为空！');
        }
        //判断子栏目
        if(target('forum/Plate')->loadList(array(), $plateId)){
            $this->error('请先删除子板块！');
        }
        //删除板块操作
        if(target('forum/Plate')->delData($plateId)){
            $this->success('板块删除成功！');
        }else{
            $msg = target('forum/Plate')->getError();
            if(empty($msg)){
                $this->error('栏目删除失败！');
            }else{
                $this->error($msg);
            }
        }
    }
}

